/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chess;

import javax.swing.ImageIcon;
import javax.swing.JButton;
/**
 *
 * @author Alejandro
 */
public class Pawn extends Token{
    
    public Pawn (int column, int row, int color, JButton [][] board, 
            Token [][] tokens){
        ImageIcon[] iconos = {new ImageIcon("src/imagenes/Scourge/ghoul-P50.gif")
                        ,new ImageIcon("src/imagenes/Legion/wisp-P50.gif")};
        this.setRow(row);
        this.setIcon(iconos);
        this.setColumn(column);
        this.setColor(color);
        this.board = board;
        this.colocarFicha();
        this.tokens = tokens;
        this.setType("P");
        this.setPass(false);
    }

    @Override
    public void moveToken(int newColumn, int newRow){
        int F = this.getRow();
        int C = this.getColumn();
        int Color = this.getColor();
        boolean hit = tokens[newColumn][newRow] != null;
        enemy = hit && tokens[newColumn][newRow].getColor() != this.getColor();
        if ((newRow == F + 1 - 2*Color && newColumn == C && !hit) ||
            (F == 1 + 5*Color && newRow == F + 2 - 4*Color && newColumn == C && !hit) 
            || (newRow == F + 1 - 2*Color && (Math.abs(newColumn-C) == 1) && enemy)){
            for (int j=0; j<8; j++){
                for (int i=0; i<8; i++){
                    try{
                        if (tokens[j][i].getType().equals("P")){
                            tokens[j][i].setPass(false);
                        }
                    }
                    catch(Exception ex){
                        continue;
                    }
                }
            }
            if (F == 1 + 5*Color && newRow == F + 2 - 4*Color && newColumn == C && !hit){
                this.setPass(true);
            }
            board[C][F].setIcon(null);
            this.setRow(newRow);
            this.setColumn(newColumn);
            this.colocarFicha();
            this.setCorrectMove(true);
        }
        else if (newRow == F + 1 - 2*Color && (Math.abs(newColumn-C) == 1) && !enemy && tokens[newColumn][newRow -1 + 2*Color] != null && tokens[newColumn][newRow -1 + 2*Color].getPass() && tokens[newColumn][newRow -1 + 2*Color].getType().equals("P") && tokens[newColumn][newRow -1 + 2*Color].getColor() != this.getColor()){
            board[newColumn][newRow -1 + 2*Color].setIcon(null);
            board[C][F].setIcon(null);
            this.setRow(newRow);
            this.setColumn(newColumn);
            this.colocarFicha();
            this.setCorrectMove(true);
        }
            else{
                this.setCorrectMove(false);
            }
    }
    
    private void colocarFicha(){
        board[this.getColumn()][this.getRow()].setIcon(this.getIcon()[this.getColor()]);
    }
}
